import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
    state:{
        asideList:JSON.parse(localStorage.getItem('asideList'||'[]')),
        token:localStorage.getItem('token',''),
        tokenHead:localStorage.getItem('tokenHead',''),
        jsession_id:localStorage.getItem('jsession_id',''),
        gh:'',
        type:'',
        role:localStorage.getItem('role',''),
        info:JSON.parse(localStorage.getItem('info'||'[]')),
        todoListNumber:0,
        todoList:JSON.parse(localStorage.getItem('todoList'||'[]')),
        todoCount:localStorage.getItem('todoCount',''),
        isAdmin:localStorage.getItem('isAdmin',''),
        // todoCount:0,
        // todoList:JSON.parse(localStorage.getItem('todoList'||'[]'))
    },
    mutations:{
        setRole(state,data){
            state.role = data;
            localStorage.setItem('role',data)
        },
        setAsideList(state,data){
            state.asideList = data;
            localStorage.setItem('asideList',JSON.stringify(data))
        },
        setToken(state,data){
            state.token = data;
            localStorage.setItem('token',data)
        },
        setTokenHead(state,data){
            state.tokenHead = data;
            localStorage.setItem('tokenHead',data)
        },
        setJsession(state,data){
            state.jsession_id = data;
            localStorage.setItem("jsession_id", data);
        },
        setGh(state,data){
            state.gh = data;
        },
        setType(state,data){
            state.type = data;
        },
        setInfo(state,data){
            state.info = data;
            localStorage.setItem('info',JSON.stringify(data))
        },
        setTodoListNumber(state,data){
          state.todoListNumber=data
        },
        decreaseTodoListNumber(state){
           state.todoListNumber-=1
        },
        getTodoList(state){
          axios.get('http://139.224.239.35:8090/teacher/getTeacherDealt',{
            headers:{
              token:localStorage.getItem('token',''),
              tokenHead:localStorage.getItem('tokenHead',''),
              jsession_id:localStorage.getItem('jsession_id','')
            }
          }).then((res)=>{
            console.log(res)
            state.todoList=res.data.data
            localStorage.setItem('todoList',JSON.stringify(res.data.data))
            if(state.role==1){
              state.todoCount=res.data.data.backList.length+res.data.data.phaseList.length+res.data.data.closeList.length
              localStorage.setItem('TodoCount',state.todoCount)
            }
            //学校管理员提示数量
            if(state.role==2){
              state.todoCount=res.data.data.data.batchId.length+res.data.data.data.closeId.length
              localStorage.setItem('TodoCount',state.todoCount)
            }
            //学校管理员提示数量
            if(state.role==3){
              state.todoCount=res.data.data.batchId.length+res.data.data.closeId.length+res.data.data.needDealtPostProject.length+res.data.data.phaseId.length
              localStorage.setItem('TodoCount',state.todoCount)
              state.isAdmin=res.data.data.admin
              localStorage.setItem('isAdmin',res.data.data.admin)
            }
            console.log(state.todoList)
          })
        }

    },
    getters:{

    },
    actions:{

    }
})
